<?php
/**
 * 多账号功能，包括绑定账号，解绑账号，举报自动分配等功能
 */

namespace App\Utils;


use Illuminate\Support\Facades\DB;

class MultipleAccounts
{
    // 最大副卡个数
    const MAX_ACCOUNT_COUNT = 4;
    // 主卡
    private $primary_uid;
    // 副卡
    private $secondary_uids;
    
    private $result = array();
    
    /**
     * MultipleAccounts constructor.
     * 初始化，必须传入主卡用户的uid
     *
     * @param $primary_uid
     */
    public function __construct($primary_uid = '')
    {
        // if (!$primary_uid){
        //     exit('没有用户编号');
        // }
        $this->primary_uid = $primary_uid ? $primary_uid : '';
        // 根据主卡获取副卡uid
        $this->getSecondarys($this->primary_uid);
    }
    
    /**
     * 获取副卡的uid
     * @param $primary_uid
     */
    private function getSecondarys($primary_uid){
        
        $this->secondary_uids = DB::table('paike_multi_accounts')->where('pid', '=', $primary_uid)->lists('uid');
    }
    
    /**
     * 绑定副卡
     *
     * @param $uid
     *
     * @return bool
     */
    public function bindSecond($uid)
    {
        // 检查主卡，是否可以绑定副卡
        if ($this->isSecond($this->primary_uid)){
            
            return false;
        }
        if (count($this->secondary_uids) >= $this::MAX_ACCOUNT_COUNT){
            return false;
        }
        // 绑定副卡操作
        $second_status = DB::table('paike_multi_accounts')->save([
            'uid'       => $uid,
            'pid'       => $this->primary_uid,
            'order'     => 0
        ]);
        
        if ($second_status){
            
            return true;
        }
        
        return false;
    }
    
    /**
     * 解绑副卡
     *
     * @param $uid
     *
     * @return bool
     */
    public function unbindSeconds($uid){
    
        if ($this->isSecond($uid)){
        
            // 绑定副卡操作
            $second_status = DB::table('paike_multi_accounts')->where('uid', '=', $uid)->where('pid', '=', $this->primary_uid)->delete();
        
            if ($second_status){
                
                return true;
            }
        }
        
        return false;
    }
    
    
    /**
     * 获取用户待审核举报数量
     *
     * @param $uids
     *
     * @return
     */
    private function getJubaoCountInfo($uids){
    
        if (is_array($uids)){
            return DB::table('report_list as a')->select(DB::raw('a.uid, if(b.c != 0, b.c, 0) as count'))
                ->leftJoin(DB::raw('(select uid, count(*) as c from report_list where status = 0 group by uid) b'), 'a.uid', '=', 'b.uid')
                ->whereIn('a.uid', $uids)->groupBy('a.uid')
                ->get();
        }else {
            return DB::table('report_list')->select(DB::raw('uid, count(*) as count'))
                ->where('uid', '=', $uids)
                ->where('status', '=', 0)
                ->first();
        }
        
    }
    
    /**
     * 检查是否是副卡，不是副卡才能当主卡
     *
     * @param $uid
     *
     * @return bool
     */
    public function isSecond($uid){
        
        return DB::table('paike_multi_accounts')->where('uid', '=', $uid)->count() === 1;
    }
    
    /**
     * 根据待审核数量，返回最小的uid
     *
     * @param $jubao_counts
     *
     * @return int|string
     */
    private function getMinCountUid($jubao_counts){
        
        $min_count_uid = '';
        $min_count = 0;
        $i = 0;
        
        foreach ($jubao_counts as $jubao_key =>$jubao_count){
            
            if ($i == 0){
                $min_count_uid = $jubao_key;
                $min_count = $jubao_count;
            }else{
                
                if ($jubao_count < $min_count){
                    
                    $min_count_uid = $jubao_key;
                }
            }
            
            $i++;
            
        }
        
        return $min_count_uid;
    }
    
    
    /**
     * 举报信息自动分配uid
     *
     */
    public function autoDistrib()
    {
        $seconds_jubaos = array();
        
        // 1. 主号待审核举报信息
        $primary_jubao_info = $this->getJubaoCountInfo($this->primary_uid);
        // print_r($primary_jubao_info);exit;
        
        if (isset($primary_jubao_info->count) && $primary_jubao_info->count > 0){
            
            $seconds_jubaos[$primary_jubao_info->uid] = $primary_jubao_info->count;
            // 主号有待审核举报,查找是否有副卡
            if (count($this->secondary_uids)){
                // 查找副卡的待审核记录，并记录
                $seconds_jubao_info = $this->getJubaoCountInfo($this->secondary_uids);
                
                foreach ($seconds_jubao_info as $secondary_info){
    
                    if (isset($secondary_info->count)){
                        $seconds_jubaos[$secondary_info->uid] = $secondary_info->count;
                    }
                }
                // 对比主卡待审核数量，取最小值的uid
                // print_r($seconds_jubaos);
                return ['status' => true, 'uid' => $this->getMinCountUid($seconds_jubaos)];
            }else {
                // 没有副卡，返回主号的uid
                return ['status' => true, 'uid' => $this->primary_uid];
            }
        }else{
            // 直接返回主号的uid
            return ['status' => true, 'uid' => $this->primary_uid];
        }
    }
}